// 爱达普测试服
var adpBaseUrl = '';
// 爱思测试服
var i4BaseUrl = '';
var environment = 'test';
var loginUrl = {};
(function () {
    if (environment == 'test') {
        adpBaseUrl = 'https://test-usercenter.i5.cn/box';
        i4BaseUrl = 'https://test-app4.i4.cn';
        loginUrl = {
            /******************************* 测试 *************************************/
            retrieveUrl: 'https://test-greenapi.i5.cn',
            userUrl: 'https://test-sdk.i5.cn',
            i5UserUrl: 'https://test-usercenter.i5.cn',
            partsUrl: 'https://test-parts.i5.cn',
            greenapi: 'https://test-greenapi.i5.cn',
            shortUrl: 'https://test-url.i5.cn',
            bbsUrl: 'https://test-bbsapi.i5.cn',
            adzUrl: 'https://test-diyapi.i5.cn',
            i4Url: 'http://120.27.160.46:81'
        };
    }
    if (environment == 'pre') {
        adpBaseUrl = 'https://pre-usercenter.i5.cn/box';
        i4BaseUrl = 'https://pre-app4.i4.cn';
        loginUrl = {
            /******************************* 测试 *************************************/
            retrieveUrl: 'https://pre-greenapi.i5.cn',
            userUrl: 'https://pre-sdk.i5.cn',
            i5UserUrl: 'https://pre-usercenter.i5.cn',
            partsUrl: 'https://pre-parts.i5.cn',
            greenapi: 'https://pre-greenapi.i5.cn',
            shortUrl: 'https://pre-url.i5.cn',
            bbsUrl: 'https://pre-bbsapi.i5.cn',
            adzUrl: 'https://pre-diyapi.i5.cn',
            i4Url: 'http://120.27.160.46:81'
        };
    }
    if (environment == 'prod') {
        adpBaseUrl = 'https://usercenter.i5.cn/box';
        i4BaseUrl = 'https://app4.i4.cn'
        loginUrl = {
            /******************************* 正式 *************************************/
            retrieveUrl: 'https://greenapi.i5.cn',
            userUrl: 'https://usercenter.i4.cn',
            i5UserUrl: 'https://usercenter.i5.cn',
            partsUrl: 'https://parts.i5.cn',
            greenapi: 'https://greenapi.i5.cn',
            shortUrl: 'https://url.i5.cn',
            bbsUrl: 'https://bbsapi.i5.cn',
            adzUrl: 'https://diyapi.i5.cn',
            i4Url: 'https://app4.i4.cn'
        };
    }
})();
// 发送get请求
var GET = function (url, successMethod, errorMethod) {
    $.ajax({
        url: url,
        data: {},
        headers: {json: localStorage.getItem('token') || ''},
        timeout: 10000,
        type: 'get',
        xhrFields: {
            withCredentials: true // 允许跨域请求时携带COOKIE
        },
        dataType: 'json',
        success: function (data) {
            successMethod(data);
        },
        error: function (errorObj) {
            console.error(errorObj);
            errorMethod(errorObj);
        }
    });
};
// 发送post请求
var POST = function (url, data, successMethod, errorMethod) {
    $.ajax({
        url: url,
        data: data,
        headers: {json: localStorage.getItem('token') || ''},
        timeout: 10000,
        type: 'post',
        xhrFields: {
            withCredentials: true // 允许跨域请求时携带COOKIE
        },
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            successMethod(data);
        },
        error: function (errorObj) {
            console.error(errorObj);
            errorMethod(errorObj);
        }
    });
};

/**
 * 初始化
 * 获取验机分余额、本机是否存在有效验机记录、价格信息
 * @param version 爱思版本号
 * @param serial 手机或平板等设备序列号
 * @param successMethod 请求成功要执行的操作
 */
var InitUserInfoByHref = function (version, serial, successMethod, failedMethod) {
    console.log(window.location.search);
    if (window.location.search) {
        var arr = window.location.href.split('?').pop().split('#').shift().split('&');
        for (var i = arr.length - 1; i >= 0; i--) {
            if (arr[i].match('json=')) {
                localStorage.setItem('token', arr[i].split('json=')[1]);
            }
            if (arr[i].match('clientType=')) {
                localStorage.setItem('elVersion', arr[i].split('clientType=')[1]);
            }
            if (arr[i].match('pc_vs=')) {
                localStorage.setItem('i4Version', arr[i].split('pc_vs=')[1]);
            }
            if (arr[i].match('serial=')) {
                localStorage.setItem('deviceSerial', arr[i].split('serial=')[1]);
            }
            if (arr[i].match('reCheck=')) {
                localStorage.setItem('reCheck', arr[i].split('reCheck=')[1]);
            }
        }
        if (!window.location.search.match('reCheck=')) {
            localStorage.setItem('reCheck', '0');
        }
    }
    // version = '7.98.57.001';
    // serial = 'F2LVHQLKJCLP';
    version = localStorage.getItem('i4Version');
    serial = localStorage.getItem('deviceSerial');
    // hasReport 是否有有效的报告 0 否  1 是（免费）
    // lastReportDate 最后验机时间
    // checkPrice 单次验机付费分数
    // frequency 验机分余额
    GET(i4BaseUrl + '/checkPrice.xhtml?pc_vs=' + version + '&serial=' + serial, function (res) {
        if (res.code === 0) {
            successMethod(res.data);
        } else {
            failedMethod(res.data);
            localStorage.removeItem('token');
        }
    }, failedMethod);
};
